package ru.narod.amidoriya.app.sushibaramidoriya;
import android.widget.*;
import android.view.*;
import android.annotation.SuppressLint;
import android.content.*;
import java.util.*;
import java.text.*;
import java.text.ParseException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.*;
import java.io.*;
import java.net.*;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.net.*;
import android.net.*;
import android.os.*;


public class GalleryAdapter extends BaseAdapter {
	private Context mContext;
	private List<? extends Map<String, ?>> mData;
	Integer mAktionNew;
	private String picurl;
	private String gaction_pic;
	private String gaction_new;
	ImageView imageView;
	ProgressBar progbars;
	private Integer seeFlagAktion;
	private int seeFlagAktionNew;
	
	public GalleryAdapter(Context c, List<? extends Map<String, ?>> gdata) {
		mContext = c;
		mData = gdata;
		
	}

	@Override
	public int getCount()
	{
		
		return  mData.size();
	}

	@Override
	public Object getItem(int position)
	{
		
		return mData.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		
	
		View galleryView;
		 

				////
				if (convertView == null) {
					galleryView = new View(mContext);
					LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
					galleryView = inflater.inflate(R.layout.gallery_items, parent, false);
				} else {
					galleryView = (View) convertView;
				}


				imageView = (ImageView) galleryView.findViewById(R.id.imageitemgallery);				
				TextView textView = (TextView) galleryView.findViewById(R.id.textitemgallery);
				TextView textAktionNew = (TextView) galleryView.findViewById(R.id.textaktionnew);
				progbars = (ProgressBar) galleryView.findViewById(R.id.prbar);
				
			////	
		try {
			picurl = mData.get(position).get("gfilepicPathUrl").toString();
			gaction_pic = mData.get(position).get("gaction_pic").toString();
			gaction_new = mData.get(position).get("gaction_new").toString();
			if (gaction_pic == null)
			{
				gaction_pic = "";
			}
			if (gaction_pic.equals("")) {

			} 
			else
			{
					new DownloadImageTask(imageView).execute(picurl);		
			}

		}
		catch (Exception e) {
			
		}

		try {
			textView.setText(mData.get(position).get("gmessage").toString());	
		}
		catch (Exception e) {
				textView.setText("");	
		}
		try {
			if (gaction_new.equals("1")) {
				textAktionNew.setVisibility(View.VISIBLE);
			} else {
				textAktionNew.setVisibility(View.GONE);	
			}
		}
		catch (Exception e) {
			textAktionNew.setVisibility(View.GONE);	
		}
		
		return galleryView;
		
	}
	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage) {
			this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls) {
			String urlsimage = urls[0];
			Bitmap image = null;
			try {
				image = BitmapFactory.decodeStream((InputStream)new URL(urlsimage).getContent());	
			} catch (Exception e) {
				
			}
			return image;
		}

		protected void onPostExecute(Bitmap result) {
			bmImage.setImageBitmap(result);
		}
	}

}
